package offer;


public class deleteNode_18 {
    public ListNode deleteNode(ListNode head, int val) {
        if(head == null) return null;
        if(head.val == val){
            return head.next;
        }
        ListNode pre = new ListNode(-1);
        pre.next = head;
        ListNode cur = head;
        while (cur != null){
            if(cur.val == val){
                ListNode next = cur.next;
                pre.next = next;
                cur.next = null;
                break;
            }
            cur = cur.next;
            pre = pre.next;
        }
        return head;

    }

    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
